package com.sdlg.mapper;

import com.sdlg.pojo.Order;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author sdlg
 * @since 2024-12-26
 */
public interface OrderMapper extends BaseMapper<Order> {
    //今日预约数
    @Select("SELECT COUNT(*) FROM t_order WHERE order_date=#{today}")
    public long getCountToday(@Param("today") String today);
    //本周预约数
    @Select("SELECT COUNT(*) FROM t_order WHERE order_date between #{firstDayThisWeek} and #{today}")
    public long getCountWeek(@Param("today") String today,@Param("firstDayThisWeek") String firstDayThisWeek);

    //本月预约数
    @Select("SELECT COUNT(*) FROM t_order WHERE order_date between #{p2} and #{today}")
    public long getCountMonth(@Param("today") String today,@Param("p2") String p2);

    //今日到诊数
    @Select("SELECT COUNT(*) FROM t_order WHERE order_date=#{today} and order_status='已归还'")
    public long getCountTodayEnd(@Param("today") String today);
    //本周到诊数
    @Select("SELECT COUNT(*) FROM t_order WHERE order_date between #{firstDayThisWeek} and #{today} and order_status='已归还'")
    public long getCountWeekEnd(@Param("today") String today,@Param("firstDayThisWeek") String firstDayThisWeek);

    //本月到诊数
    @Select("SELECT COUNT(*) FROM t_order WHERE order_date between #{p2} and #{today} and order_status='已归还'")
    public long getCountMonthEnd(@Param("today") String today,@Param("p2") String p2);

    @Select("SELECT s.name,s.remark,c.ct bookitem_count,(c.ct/(SELECT COUNT(*) FROM t_order))" +
            " proportion FROM t_bookitem s,(SELECT COUNT(*) ct,bookitem_id sid FROM t_order GROUP BY bookitem_id LIMIT 0,10) c " +
            "WHERE s.id=c.sid")
    public List<Map<String,Object>> hotSetmeal();


    @Update("UPDATE t_order SET order_status = '已归还' WHERE id = #{id}")
    boolean updateBookStatusById(@Param("id") Integer id);
    @Update("UPDATE t_order SET order_status = '已提醒' WHERE id = #{id}")
    boolean remarkBookStatusById(@Param("id") Integer id);
    @Update("UPDATE t_order JOIN t_bookitem ON t_order.bookitem_id = t_bookitem.id SET order_status = " +
            "CONCAT('罚款', t_bookitem.price, '元') WHERE t_order.id = #{id};")
    boolean overtimeBookStatusById(@Param("id") Integer id);

    @Update("UPDATE t_order SET order_status = '已支付罚款' WHERE id = #{id}")
    boolean buyBookStatusById(@Param("id") Integer id);

    @Update("UPDATE t_order\n" +
            "SET order_status = CASE \n" +
            "                    WHEN DATEDIFF(CURDATE(), order_date) > 30 AND DATEDIFF(CURDATE(), order_date) < 60 THEN '已提醒'\n" +
            "                    WHEN DATEDIFF(CURDATE(), order_date) > 60 THEN CONCAT('罚款', " +
            "(SELECT price FROM t_bookitem WHERE id = t_order.bookitem_id), '元')\n" +
            "                    ELSE '未归还'\n" +
            "                    END\n" +
            "WHERE order_status = '未归还' or order_status = '已提醒';")
    boolean updateOrderAllStatus();

    @Update("UPDATE t_order SET delay_time = DATEDIFF(CURDATE(), order_date);")
    boolean getDelayTime();

    @Select("SELECT COUNT(*) AS record_count\n" +
            "FROM t_order\n" +
            "WHERE DATE(order_date) = #{date};")
    Integer getAllReservations(@Param("date") LocalDate localDate);
}
